package com.time.interceptor;

import com.time.domain.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 张龙佼
 * @Description 登录拦截器
 * @since 2020-10-14
 */

public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o)
            throws Exception {

        User userSession = (User) req.getSession().getAttribute("USER_IN_SESSION");
        if (userSession == null) {
            // 获取拦截地址并跳转至登录页面
            req.setAttribute("url", req.getRequestURI());
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
            return false;
        }
        return true;
    }

}
